//
//  MusicPlayController.m
//  musicPlayer
//
//  Created by 遇水 on 15/6/18.
//  Copyright (c) 2015年 YS. All rights reserved.
//

#import "MusicPlayController.h"
#import "MusicTool.h"
#import "MusicsListController.h"
#import "MusicModel.h"
#import <AVFoundation/AVFoundation.h>
#import "HMAudioTool.h"
#import "AlbumsViewController.h"
#import "MoreViewController.h"



@interface MusicPlayController ()
- (IBAction)playOrPause;
- (IBAction)previous;

- (IBAction)next;

- (IBAction)download;
- (IBAction)albums;
- (IBAction)musicList;
- (IBAction)more;

@property (nonatomic, strong) MusicModel *playingMusic;     //当前正在播放的歌曲
@property (nonatomic, strong) AVAudioPlayer *player;    //当前正在播放的播放器
@property (nonatomic, strong) NSTimer    *currentTimer; //当前音乐的定时器


@property (weak, nonatomic) IBOutlet UIImageView *singerImage;//歌手图片
@property (weak, nonatomic) IBOutlet UILabel *musicName; //歌曲名称
@property (weak, nonatomic) IBOutlet UILabel *currenttime;//歌曲的当前时间
@property (weak, nonatomic) IBOutlet UILabel *singerName;   //歌手名称

@property (weak, nonatomic) IBOutlet UIButton *playOrPuaseButton;//播放或者暂停按钮

@property (weak, nonatomic) IBOutlet UILabel *musicTime; //歌曲总时长

@property (weak, nonatomic) IBOutlet UIView *slider;
@property (weak, nonatomic) IBOutlet UIImageView *zhe_zhao_jin_du;

@property (weak, nonatomic) IBOutlet UIView *progressView;
@property (weak, nonatomic) IBOutlet UIImageView *zhe_zhao;

//@property (weak, nonatomic) IBOutlet UIView *slider;    //滑块
//@property (weak, nonatomic) IBOutlet UIView *progressView;//滑块当前的进度
//@property (weak, nonatomic) IBOutlet UIImageView *zhe_zhao_jin_du;//图像上的进度
//@property (weak, nonatomic) IBOutlet UIImageView *zhe_zhao;//图像上的遮罩

@end

@implementation MusicPlayController

- (void)viewDidLoad {
    [super viewDidLoad];
        //隐藏navigationbar
//    self.navigationController.navigationBar.hidden = YES;
//    [self setPlayingMusic];
    
    NSLog(@"--------player%@--------playingMusic%@",_player,_playingMusic);
}

/**
 *  设置音乐数据
 *
 */
-(void)setPlayingMusic
{
    
    //如果选择的是当前正在播放的这首歌那么久不用设置了
    //初始状态使 工具类中的playingMusic是空的
//    if (self.playingMusic ==[MusicTool playingMusic]) {
//        return;
//    }
    
    //首次进入这个界面的时候这里是没有值的,选择音乐后，这里就不是空了，当选择的正是现在正在播放的，直接跳出
    //当传入的音乐是当前播放的音乐，(首次传入的时候，当前播放的音乐这里，可以为空)
    if((self.playingMusic==[MusicTool playingMusic]) && self.playingMusic!= nil){
        [self addTimer];
        return;
    }else{
        [self removePlayingMusic];
    }
    
    //设置界面数据
    self.playingMusic = [MusicTool playingMusic];
    if (self.playingMusic==nil) {
        self.singerImage.image = [UIImage imageNamed:@"layer-9"];
    }else{
         self.singerImage.image = [UIImage imageNamed:self.playingMusic.icon];
        self.musicName.text = self.playingMusic.name;
        self.singerName.text = self.playingMusic.singer;
    }


    //开始播放
    if(_playingMusic != nil){
    self.player = [HMAudioTool playMusic:_playingMusic.filename];
    self.player.delegate = self;
    //设置时长
    self.musicTime.text = [self stringWithTime:self.player.duration];
#warning 此处进行定时器的更新
    //开启定时器
        [self addTimer];
      
        
    }
}

//添加定时器
-(void)addTimer
{
    //
//    if (self.player.isPlaying==NO) {
//        return;
//    }
    
    self.currentTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:self.currentTimer forMode:NSRunLoopCommonModes];
    
    
}
//更新进度的
-(void)updateCurrentTime
{
    
        //跟新界面的进度值
    double progress = self.player.currentTime /self.player.duration;
    //计算滑块最大的x值
    CGFloat sliderMaxX = self.view.width - self.slider.width;   //最大的宽度
    self.slider.x = sliderMaxX * progress;
    NSLog(@"%@",@(self.slider.x));
//    
//    self.slider.x = sliderMaxX *progress;
//    
//    //跟新滑块的当前的进度(跟随滑块增加的)
    self.progressView.width = self.slider.center.x;
//
//    //根性图像上的遮罩位置
    CGFloat zhe_zhao_max_x = self.view.width - self.zhe_zhao_jin_du.width;//318
    self.zhe_zhao_jin_du.x = zhe_zhao_max_x *progress;  //318*0.018

//   NSLog(@"------当前线程%@",[NSThread currentThread]);

    self.zhe_zhao.width = self.zhe_zhao_jin_du.center.x;
    
    //跟新显示的当前时间
    [self.currenttime setText:[self stringWithTime:self.player.currentTime]];
    
}



-(void)removeCurrentTimer
{
    //清除定时器
    [self.currentTimer invalidate];
    self.currentTimer = nil;
}



//清除当前正在播放的音乐
-(void)removePlayingMusic
{
    self.playOrPuaseButton.selected = NO;
    self.singerImage.image = [UIImage imageNamed:@"layer-9"];
    self.musicName.text = nil;
    self.musicTime.text = nil;
    
    //停止当前正在播放的音乐
    [HMAudioTool stopMusic:self.playingMusic.filename];
    self.player = nil;
    
    //停止定时器
    [self removeCurrentTimer];
    
    
}


/**
 *  播放器是按照秒计算的，所以这里要转化一下
 *
 */
- (NSString *)stringWithTime:(NSTimeInterval)time
{
    int minute = time / 60;
    int second = (int)time % 60;
    return [NSString stringWithFormat:@"%d:%d", minute, second];
}


#pragma mark - playingClick


/**
 *  播放
 *
 *  @param sender
 */
- (IBAction)playOrPause
{
    if (_playingMusic ==nil) {
        [self next];
        return;
          }
    //当前正在播放
    if(self.player.isPlaying){
        //正在播放就是选中状态，暂停音yue

        self.playOrPuaseButton.selected = NO;
        [HMAudioTool pauseMusic:self.playingMusic.filename];
        [self removeCurrentTimer];
    
        
    }else{
        self.playOrPuaseButton.selected = YES;
        [HMAudioTool playMusic:self.playingMusic.filename];
        [self addTimer];
    }
}
/**
 *  上一首
 *
 *  @param sender <#sender description#>
 */
- (IBAction)previous
{
    [self removePlayingMusic];
    
    [MusicTool setPlayingMusic:[MusicTool previousMusic]];
    
    [self setPlayingMusic];
}

- (IBAction)next {
    //首先清除现在播放的音乐
    [self removePlayingMusic];
    self.playOrPuaseButton.selected = YES;
    [MusicTool setPlayingMusic:[MusicTool nextMusic]];      //获得下一首音乐
    //设置播放
    [self setPlayingMusic];
}












#pragma mark - footerClick

//下载点击事件

- (IBAction)download
{
}

- (IBAction)albums{
    //
    AlbumsViewController *ablumsVc = [[AlbumsViewController alloc]init];
    [self.navigationController pushViewController:ablumsVc animated:YES];
    
}

//点击这个按钮后，从底部上来一个控制器
- (IBAction)musicList {
   
    MusicsListController *musicList = [[MusicsListController alloc]init];
    [self.navigationController pushViewController:musicList animated:YES];

}



-(void)one{
    //    //显示musiclist界面,需要获得zhuwindow
    //    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //    window.userInteractionEnabled = NO;     //禁止用户点击
    //    MusicsListController *listVc = [[MusicsListController alloc]init];
    //    listVc.view.frame = window.bounds;
    //    //将新创建这个vc的view添加到window上去
    //    [window addSubview:listVc.view];                    //这句有错？？？？？？
    //
    //
    //    listVc.view.y = self.view.height;
    //    //通过动画加载界面
    //    [UIView animateWithDuration:1.0 animations:^{
    ////        listVc.view.y = self.view.height;       //将此控件的view的y与现有的高相同
    //        listVc.view.y = 0;
    //    } completion:^(BOOL finished) {
    //        self.view.hidden = YES;     //将现有的隐藏
    //        window.userInteractionEnabled = YES;
    //    }];
    //
}

- (IBAction)more
{
    MoreViewController *moreVC = [[MoreViewController alloc]init];
    [self.navigationController pushViewController:moreVC animated:YES];
}


-(void)viewWillDisappear:(BOOL)animated{
    //在此处清除定时器
    NSLog(@"push 之前调用");
    
    [self removeCurrentTimer];
    
}



//viewdidLoad 方法执行完毕执行这个(pop 后需要这个)
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
    [self setPlayingMusic];
}







@end
